package com.haixiaoke.saas.repository.service.impl;


import com.haixiaoke.saas.repository.exception.BizException;
import com.haixiaoke.saas.repository.mapper.HouseQuitLeaseMapper;
import com.haixiaoke.saas.repository.pojo.HouseQuitLease;
import com.haixiaoke.saas.repository.service.IHouseQuitLeaseService;
import com.haixiaoke.saas.repository.vo.HouseQuitLeaseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 公寓管理退租记录Service业务层处理
 *
 * @author qixi
 * @date 2023-04-18
 */
@Service
public class HouseQuitLeaseServiceImpl implements IHouseQuitLeaseService {


    @Autowired
    private HouseQuitLeaseMapper houseQuitLeaseMapper;

    /**
     * 查询公寓管理退租记录
     *
     * @param quitLeaseId 公寓管理退租记录主键
     * @return 公寓管理退租记录
     */
    @Override
    public HouseQuitLease selectHouseQuitLeaseByQuitLeaseId(Long quitLeaseId) {
        return houseQuitLeaseMapper.selectHouseQuitLeaseByQuitLeaseId(quitLeaseId);
    }

    /**
     * 查询公寓管理退租记录列表
     *
     * @param houseQuitLease 公寓管理退租记录
     * @return 公寓管理退租记录
     */
    @Override
    public List<HouseQuitLeaseVo> selectHouseQuitLeaseList(HouseQuitLease houseQuitLease) {
        return houseQuitLeaseMapper.selectHouseQuitLeaseList(houseQuitLease);
    }

    /**
     * 新增公寓管理退租记录
     *
     * @param houseQuitLease 公寓管理退租记录
     * @return 结果
     */
    @Override
    public int insertHouseQuitLease(HouseQuitLease houseQuitLease) {
        houseQuitLease.setCreateTime(new Date());

        if (!this.checkOrderIdUnique(houseQuitLease.getOrderId())){
            throw new BizException("当前订单已申请退租");
        }
        return houseQuitLeaseMapper.insertHouseQuitLease(houseQuitLease);
    }

    /**
     * 修改公寓管理退租记录
     *
     * @param houseQuitLease 公寓管理退租记录
     * @return 结果
     */
    @Override
    public int updateHouseQuitLease(HouseQuitLease houseQuitLease) {
        houseQuitLease.setUpdateTime(new Date());
        return houseQuitLeaseMapper.updateHouseQuitLease(houseQuitLease);
    }

    /**
     * 批量删除公寓管理退租记录
     *
     * @param quitLeaseIds 需要删除的公寓管理退租记录主键
     * @return 结果
     */
    @Override
    public int deleteHouseQuitLeaseByQuitLeaseIds(Long[] quitLeaseIds) {
        return houseQuitLeaseMapper.deleteHouseQuitLeaseByQuitLeaseIds(quitLeaseIds);
    }

    /**
     * 删除公寓管理退租记录信息
     *
     * @param quitLeaseId 公寓管理退租记录主键
     * @return 结果
     */
    @Override
    public int deleteHouseQuitLeaseByQuitLeaseId(Long quitLeaseId) {
        return houseQuitLeaseMapper.deleteHouseQuitLeaseByQuitLeaseId(quitLeaseId);
    }

    @Override
    public boolean checkHouseIdAndTenantIdUnique(String houseId, String id) {
        return houseQuitLeaseMapper.checkHouseIdAndTenantIdUnique(houseId,id) == 0;
    }

    @Override
    public boolean checkOrderIdUnique(String orderId) {
        return houseQuitLeaseMapper.checkOrderIdUnique(orderId) == 0;
    }


    /**
     * 根据订单id 查询公寓管理退租记录列表
     *
     * @param orderId 订单id
     * @return 公寓管理退租记录
     */
    @Override
    public HouseQuitLeaseVo selectHouseQuitLeaseVoByOrderId(String orderId) {
        return houseQuitLeaseMapper.selectHouseQuitLeaseVoByOrderId(orderId);
    }

    @Override
    public HouseQuitLease selectHouseQuitLeaseByOrderId(String orderId) {
        return houseQuitLeaseMapper.selectHouseQuitLeaseByOrderId(orderId);
    }



}
